﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PSDLib.Models;

namespace PSDManager
{
    public partial class ChannelInfo : UserControl
    {
        public event EventHandler OnMouseEnter;

        public event EventHandler OnMouseLeave;
        public event EventHandler OnDownloadStateChanged;
        public ManagerHelper ManagerHelper { get; set; }
        public dl_task Task { get; set; }
        public dl_task_channel Channel { get; set; }

        public ChannelInfo()
        {
            InitializeComponent();

            this.toolTip1.SetToolTip(pbStartDL, "开始下载");
            this.toolTip1.SetToolTip(pbStopDL, "暂停下载");
            this.toolTip1.SetToolTip(pbFinishDL, "结束下载");
            this.toolTip1.SetToolTip(pbDeleteDL, "删除下载");

            palMask.BackColor = Color.White;

            pbStartDL.Click += btnStartDL_Click;
            pbStopDL.Click += btnStopDL_Click;
            pbFinishDL.Click += btnFinishDL_Click;
            pbDeleteDL.Click += btnDeleteDL_Click;

            this.MouseEnter += ChannelInfo_MouseEnter;
            pbIcon.MouseEnter += ChannelInfo_MouseEnter;
            lblChannelName.MouseEnter += ChannelInfo_MouseEnter;
            pbState.MouseEnter += ChannelInfo_MouseEnter;
            lblState.MouseEnter += ChannelInfo_MouseEnter; ;
            lblSuccess.MouseEnter += ChannelInfo_MouseEnter;
            lblFail.MouseEnter += ChannelInfo_MouseEnter;
            lblline.MouseEnter += ChannelInfo_MouseEnter;
            pbStartDL.MouseEnter += ChannelInfo_MouseEnter;
            pbStopDL.MouseEnter += ChannelInfo_MouseEnter;
            pbFinishDL.MouseEnter += ChannelInfo_MouseEnter;
            pbDeleteDL.MouseEnter += ChannelInfo_MouseEnter;

            ShowButtons(false);
        }

        public void SetChannel(dl_task task, dl_task_channel channel)
        {
            Task = task;
            Channel = channel;

            lblChannelName.Text = channel.channel_name;
            lblSuccess.Text = $"下载 {channel.file_count} 个文件";
            lblFail.Text = $"失败 {channel.error_count} 个文件";

            if (channel.download_state == 0)
            {
                pbState.Image = Properties.Resources.stop_dl;
                lblState.Text = "已暂停";
                lblState.ForeColor = Color.Gray;
            }
            else if (channel.download_state == 1)
            {
                pbState.Image = Properties.Resources.start_dl;
                lblState.Text = "下载中";
                lblState.ForeColor = Color.Green;
            }
            else if (channel.download_state == 2)
            {
                pbState.Image = Properties.Resources.finish_dl;
                lblState.Text = "已结束";
                lblState.ForeColor = Color.SteelBlue;
            }

            EnableButtons();
        }

        private void EnableButtons()
        {
            pbStartDL.Image = (Channel.download_state == 0 ? Properties.Resources.start_dl : Properties.Resources.start_dl_disable);
            pbStartDL.Enabled = (Channel.download_state == 0);
            pbStopDL.Image = (Channel.download_state == 1 ? Properties.Resources.stop_dl : Properties.Resources.stop_dl_disable);
            pbStopDL.Enabled = (Channel.download_state == 1);
            pbFinishDL.Image = (Channel.download_state < 2 ? Properties.Resources.finish_dl : Properties.Resources.finish_dl_disable);
            pbFinishDL.Enabled = (Channel.download_state < 2);
            pbDeleteDL.Image = Properties.Resources.delete_dl;
            pbDeleteDL.Enabled = true;
        }

        private void btnStartDL_Click(object sender, EventArgs e)
        {
            ManagerHelper.StartPSDDownload(Task.download_device_id, null, new List<dl_task_channel> { Channel });

            OnDownloadStateChanged?.Invoke(sender, e);
        }

        private void btnStopDL_Click(object sender, EventArgs e)
        {
            ManagerHelper.StopPSDDownload(Task.download_device_id, null, new List<dl_task_channel> { Channel });

            OnDownloadStateChanged?.Invoke(sender, e);
        }

        private void btnFinishDL_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定结束该通道的下载任务？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ManagerHelper.FinishPSDDownload(Task.download_device_id, null, new List<dl_task_channel> { Channel });

                OnDownloadStateChanged?.Invoke(sender, e);
            }
        }

        private void btnDeleteDL_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定删除该通道的下载任务？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ManagerHelper.DeletePSDDownload(Task.download_device_id, null, new List<dl_task_channel> { Channel });

                OnDownloadStateChanged?.Invoke(sender, e);
            }
        }

        public void ShowButtons(bool show)
        {
            if (show)
            {
                palMask.Width = 0;
            }
            else
            {
                palMask.Width = this.Width;
            }
        }

        private void ChannelInfo_MouseEnter(object sender, EventArgs e)
        {
            //this.lblState.Text = (sender as Control).Name;
            //this.lblSuccess.Text = "enter";
            //this.ShowButtons(true);
            //this.BackColor = Color.AliceBlue;
            OnMouseEnter?.Invoke(this, e);
        }

        private void ChannelInfo_MouseLeave(object sender, EventArgs e)
        {
            //this.lblState.Text = (sender as Control).Name;
            //this.lblSuccess.Text = "leave";
            //this.ShowButtons(false);
            //this.BackColor = Color.White;
            OnMouseLeave?.Invoke(this, e);
        }
    }
}
